﻿#include "displaysetting.h"
#include "ui_displaysetting.h"

DisplaySetting::DisplaySetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DisplaySetting)
{
    ui->setupUi(this);
    ui->radioButton_four_seasons->setChecked(true);
    setStyleSheet(
                  /*checkbox样式设置*/
                  "QCheckBox::indicator { \
                      width: 40px;\
                      height: 50px;\
                  }"
                  /*未选中*/
                  "QCheckBox::indicator::unchecked {  \
                      image: url(:/img/checkBox_unselect.png);\
                  }"
                  /*选中*/
                  "QCheckBox::indicator::checked { \
                      image: url(:/img/checkBox_select.png);\
                  }"
                  );
    initImage();
}

DisplaySetting::~DisplaySetting()
{
    delete ui;

}

/*初始化图片 */
void DisplaySetting::initImage()
{
    gatherButton(); //把按钮汇总到数组里面
    for (int i=0 ;i<20;i++) {
         Scenery[i]=":/four_seasons/scenery_"+QString::number(i+1)+".PNG";
    }
    for ( int i = 0 ; i<5 ; i++ ) {

        Spring[i]=":/four_seasons/spring"+QString::number(i+1)+".PNG";
        Summer[i]=":/four_seasons/summer"+QString::number(i+1)+".PNG";
        Autumn[i]=":/four_seasons/autumn"+QString::number(i+1)+".PNG";
        Winter[i]=":/four_seasons/winter"+QString::number(i+1)+".PNG";
    }    

    for (int i = 0 ; i<20 ; i++) {
           if(i<5)
               labels[i]->setPixmap(Spring[i%5]);
           else if(5<=i&&i<10)
               labels[i]->setPixmap(Summer[i%5]);
           else if(10<=i&&i<15)
               labels[i]->setPixmap(Autumn[i%5]);
           else if(15<=i)
               labels[i]->setPixmap(Winter[i%5]);
           labels[i]->setScaledContents(true);
           labels[i]->show();
    }

    for(int i =0 ; i<20 ; i++)
    {
        checkboxes[i]->setCheckState(Qt::Checked);
    }
}

/*汇总界面的按钮到链表，方便后续操作*/
void DisplaySetting::gatherButton()
{
    labels[0] = ui->label_1;
    labels[1] = ui->label_2;
    labels[2] = ui->label_3;
    labels[3] = ui->label_4;
    labels[4] = ui->label_5;
    labels[5] = ui->label_6;
    labels[6] = ui->label_7;
    labels[7] = ui->label_8;
    labels[8] = ui->label_9;
    labels[9] = ui->label_10;
    labels[10] = ui->label_11;
    labels[11] = ui->label_12;
    labels[12] = ui->label_13;
    labels[13] = ui->label_14;
    labels[14] = ui->label_15;
    labels[15] = ui->label_16;
    labels[16] = ui->label_17;
    labels[17] = ui->label_18;
    labels[18] = ui->label_19;
    labels[19] = ui->label_20;
    checkboxes[0]= ui->checkBox_1;
    checkboxes[1]= ui->checkBox_2;
    checkboxes[2]= ui->checkBox_3;
    checkboxes[3]= ui->checkBox_4;
    checkboxes[4]= ui->checkBox_5;
    checkboxes[5]= ui->checkBox_6;
    checkboxes[6]= ui->checkBox_7;
    checkboxes[7]= ui->checkBox_8;
    checkboxes[8]= ui->checkBox_9;
    checkboxes[9]= ui->checkBox_10;
    checkboxes[10]= ui->checkBox_11;
    checkboxes[11]= ui->checkBox_12;
    checkboxes[12]= ui->checkBox_13;
    checkboxes[13]= ui->checkBox_14;
    checkboxes[14]= ui->checkBox_15;
    checkboxes[15]= ui->checkBox_16;
    checkboxes[16]= ui->checkBox_17;
    checkboxes[17]= ui->checkBox_18;
    checkboxes[18]= ui->checkBox_19;
    checkboxes[19]= ui->checkBox_20;
}

/*获取选择到的图片路径*/
void DisplaySetting::gatherSelectedImages()
{
    landscapeImages.clear();
    //先判断选择的是按季节显示还是随机显示
     //将选择的图片存到列表landscapeImages里面,方便发送给日历显示

    if(ui->radioButton_four_seasons->isChecked())
    {
        for (int i=0 ; i<20 ;i++) {
            if(checkboxes[i]->isChecked())
            {
                //按春夏秋冬四种分别存储，分别用0,1,2,3代表四个类型
                if(i<5)
                {
                    landscapeImages.append(new LandscapeImage(0,Spring[i]));
                }
                else if(i>=5 && i<10)
                {
                    landscapeImages.append(new LandscapeImage(1,Summer[i%5]));
                }
                else if(i>=10 && i<15)
                {
                    landscapeImages.append(new LandscapeImage(2,Autumn[i%5]));
                }
                else
                {
                    landscapeImages.append(new LandscapeImage(3,Winter[i%5]));
                }

            }
        }
    }
    else if(ui->radioButton_2->isChecked())
    {
        for (int i=0 ; i<20 ;i++) {
            if(checkboxes[i]->isChecked())
            {
                landscapeImages.append(new LandscapeImage(4,Scenery[i]));
            }
        }
    }
}

/*以下两个事件 由于思路更改，暂没使用*/
void DisplaySetting::on_checkBox_1_clicked(bool checked)
{
}

void DisplaySetting::on_checkBox_2_clicked(bool checked)
{
}

/*返回按钮的点击事件*/
void DisplaySetting::on_pushButton_return_clicked()
{  
    this->close();
}

/*显示规则改为随机，更换陈列显示的图片*/
void DisplaySetting::on_radioButton_2_clicked()
{
    ui->label_spring->hide();
    ui->label_summer->hide();
    ui->label_autumn->hide();
    ui->label_winter->hide();
    for (int i=0 ; i<20 ; i++) {
         labels[i]->setPixmap(Scenery[i]);
         labels[i]->setScaledContents(true);
         labels[i]->show();
    }

}

/*以下两个事件为规则单选框的点击事件*/
void DisplaySetting::on_radioButton_four_seasons_clicked()
{
    ui->label_spring->show();
    ui->label_summer->show();
    ui->label_autumn->show();
    ui->label_winter->show();
    for (int i = 0 ; i<20 ; i++) {
           if(i<5)
               labels[i]->setPixmap(Spring[i%5]);
           else if(5<=i&&i<10)
               labels[i]->setPixmap(Summer[i%5]);
           else if(10<=i&&i<15)
               labels[i]->setPixmap(Autumn[i%5]);
           else if(15<=i)
               labels[i]->setPixmap(Winter[i%5]);
           labels[i]->setScaledContents(true);
           labels[i]->show();
    }
}

void DisplaySetting::on_pushButton_confirm_clicked()
{
    gatherSelectedImages();
    //弹出提示操作成功消息框
   QMessageBox *m =new QMessageBox(this);
    m->information(NULL, QString::fromUtf8("消息提示"), QString::fromUtf8("更改成功"));
    //单选规则为四季则发送0，随机则发送1
    if(ui->radioButton_four_seasons->isChecked())
        emit sendImage(landscapeImages,0);
    else
        emit sendImage(landscapeImages,1);
}
